import { MessageBox, Message } from "element-ui";
import store from "@/store";
import axios from "axios";
// create an axios instance 跨域配置
const serviceTx = axios.create({
  baseURL: process.env.VUE_APP_BASE_API_TX, // url = base url + request url
  timeout: 120000, // request timeout
});
serviceTx.base = process.env.VUE_APP_BASE_API_TX; //代理跨域设置替换地址
// request interceptor
serviceTx.interceptors.request.use(
  (config) => {
    // do something before request is sent

    // config.headers['token'] = getToken()
    return config;
  },
  (error) => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

// response interceptor
serviceTx.interceptors.response.use(
  (response) => {
    const res = response.data;

    // if the custom code is not 20000, it is judged as an error.
    // 这里可根据实际情况，自行对响应进行拦截处理
    if (res.code != 20000) {
      MessageBox.confirm(res.msg, {
        type: "warning",
      });
      return Promise.reject(new Error(res.message || "Error"));
    } else {
      return res;
    }
  },
  (error) => {
    console.log("err" + error); // for debug
    return Promise.reject(error);
  }
);

export default serviceTx;
